﻿using System;

namespace XUtility
{
    #region IntergerClass 类型 扩展方法

    public static class IntergerEx
    {
        #region 返回 Boolean 类型
        /// <summary>
        /// 返回Boolean类型
        /// </summary>
        /// <param name="i">输入整型</param>
        /// <returns>Boolean类型</returns>
        public static Boolean ToBoolean(this Int32? i)
        {
            if(i.HasValue)
            {
                return i.Value.ToBoolean();
            }
            return false;
        }

        /// <summary>
        /// 返回Boolean类型
        /// </summary>
        /// <param name="i">输入整型</param>
        /// <returns>Boolean类型</returns>
        public static Boolean ToBoolean(this Int32 i)
        {
            Boolean bValue = true;
            if(i == 0)
            {
                bValue = false;
            }
            return bValue;
        } 
        #endregion

        #region 返回 Double 类型
        /// <summary>
        /// 返回Double类型
        /// </summary>
        /// <param name="i">输入整型</param>
        /// <returns>Double类型</returns>
        public static Double ToDouble(this Int32? i)
        {
            if(i.HasValue)
            {
                return i.Value.ToDouble();
            }
            return 0D;
        }

        /// <summary>
        /// 返回Double类型
        /// </summary>
        /// <param name="i">输入整型</param>
        /// <returns>Double类型</returns>
        public static Double ToDouble(this Int32 i)
        {
            return (Double)i;
        }
        #endregion

        #region 返回枚举类型
        /// <summary>
        /// 返回枚举类型
        /// </summary>
        /// <param name="i">输入整型</param>
        /// <returns>Double类型</returns>
        public static TEnum ToEnum<TEnum>(this Int32? i) where TEnum : struct
        {
            if(i.HasValue)
            {
                return i.Value.ToEnum<TEnum>();
            }
            throw new ArgumentException("参数不能为空");
        }

        /// <summary>
        /// 返回枚举类型
        /// </summary>
        /// <param name="i">输入整型</param>
        /// <returns>Double类型</returns>
        public static TEnum ToEnum<TEnum>(this Int32 i) where TEnum : struct
        {
            return i.ToString().ToEnum<TEnum>();
        }
        #endregion
    }
    #endregion
}
